package net.mysgame.czfun.controller;

import net.mysgame.czfun.module.user.data.CzUser;
import net.mysgame.czfun.service.UserService;
import net.mysgame.czfun.utils.IdUtils;
import net.mysgame.czfun.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by manatea on 2016/5/19.
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {
    public static Logger logger = LoggerFactory.getLogger(UserController.class);


    @Resource(name = "userService")
    private UserService userService;

    public
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ResponseBody
    Map<String,Object> getUser() {
        Map<String,Object> map = new HashMap<String, Object>();
        CzUser czUser = userService.getUserByName("manatea");
        System.out.println(czUser);
        map.put("user",czUser);
        return map;
    }

    public
    @RequiresUser
    @RequestMapping(value = "/loginMove" ,method = RequestMethod.GET)
    @ResponseBody
    Map<String,Object> loginMove() {
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("result",userService.loginMove());
        return map;
    }

    public
    @RequestMapping(value = "/insert", method = {RequestMethod.GET})
    ModelAndView insertUser() {
        ModelAndView modelAndView = new ModelAndView("insertUser");
        CzUser czUser = new CzUser(IdUtils.generateUserId("manatea"), "manatea", ShiroUtils.encodeToHex("19921221"), (byte) 4, false, "hhaa", "hqhq");
        if (userService.addOrUpdateUser(czUser)) {
            modelAndView.addObject("success", "true");
        }
        return modelAndView;
    }

    public
    @RequestMapping(value = "/show", method = RequestMethod.GET)
    String show() {
        logger.debug("this is a new method");
        return "findUser";
    }

}
